package cn.com.xinjinfu.pdoulcProduct.activity.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;
import cn.com.xinjinfu.pdoulcProduct.view.ProgressWebView;


/**
 * @Description:WebView界面，带自定义进度条显示
 * @author http://blog.csdn.net/finddreams
 */ 
public class BaseWebActivity extends Activity {

	protected ProgressWebView mWebView;
	private String url;
	private LoadStatusBox loadbox;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baseweb);
		mWebView = (ProgressWebView) findViewById(R.id.baseweb_webview);
		mWebView.getSettings().setJavaScriptEnabled(true);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		url = bundle.getString("url");
		loadbox = (LoadStatusBox) this.findViewById(R.id.loadStatusBox);
		loadbox.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				netCheck();
			}
		});
		loadbox.loading();
	}

	private void netCheck(){
		if (!DataHandler.isNetworkConnected(this)) {
			loadbox.failed();
			ToastManager.show(this, "请检查网络连接");
			return;
		}else {
			initData();
			loadbox.success();
		}
	}

	protected void initData() {
		mWebView.loadUrl(url);
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
										String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				//这里进行无网络或错误处理，具体可以根据errorCode的值进行判断，做跟详细的处理。
				L.d("errorCode==========" + errorCode);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				//获取到title
				TitleManager.showTitle(BaseWebActivity.this, mWebView.getTitle(), "");
			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWebView = null;
	}

}
